package problem67;

//67.二进制求和
//https://leetcode.cn/problems/add-binary/description/

class Solution {
    public String addBinary(String a, String b) {
        int n = a.length()-1, m = b.length()-1;
        int t = 0;
        StringBuffer s = new StringBuffer();
        while(n >= 0 || m >= 0) {
            int x = n >= 0 ? a.charAt(n)-'0' : 0;
            int y = m >= 0 ? b.charAt(m)-'0' : 0;
            int sum = (x + y + t) % 2;
            t = (x + y + t) / 2;
            s.append(sum);
            n--;
            m--;
        }
        if(t != 0) s.append(t);

        return s.reverse().toString();
    }
}